package com.xinqi.common.rocketmq.util;

import com.xinqi.common.base.utils.json.JSON;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class ByteUtils {
    public static Charset charset = StandardCharsets.UTF_8;

    public static String byteBufferToStr(ByteBuffer byteBuffer) {
        // 将ByteBuffer转换成String
        return charset.decode(byteBuffer).toString();
    }

    public static <T> T byteToObj(ByteBuffer byteBuffer, Class<T> clazz) {
        // 将ByteBuffer转换成String
        return JSON.parse(byteBufferToStr(byteBuffer), clazz);
    }

    public static byte[] obj2Byte(Object obj) {
        return str2Byte(JSON.stringify(obj));
    }

    public static byte[] str2Byte(String str) {
        return str.getBytes(charset);
    }

}
